﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Microsoft.Web.Mvc;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class FloorController : Controller
    {
        //
        // GET: /Floor/
        private readonly IFloorRepository floorRepository;
        private readonly IUnitOfWork unitOfWork;
        public FloorController(IFloorRepository floorRepository, IUnitOfWork unitOfWork)
        {
            this.floorRepository = floorRepository;
            this.unitOfWork = unitOfWork;
        }
        private IList<FloorInfoModel> GetFloor()
        {
            var floor = floorRepository.GetAll();
            IList<FloorInfoModel> floorInfoModels = (from f in floor
                                                     select new FloorInfoModel
                                                                {
                                                                    ID = f.ID,
                                                                    Name = f.Name,
                                                                    Remark =f.Remark
                                                                }
                                                    ).ToList() ;
            return floorInfoModels;
        }

        public ActionResult ShowAllFloor()
        {
            return View(GetFloor());
        }

        [GridAction]
        public ActionResult TeletrikAjax()
        {
            return View(new GridModel(GetFloor()));
        }

        [HttpPost]
        [GridAction]
        public ActionResult AddFloor()
        {
            var floorInfoModel = new FloorInfoModel();
            if (TryUpdateModel(floorInfoModel))
            {
                var floor = new Floor();
                ModelCopier.CopyModel(floorInfoModel, floor);
                floorRepository.Add(floor);
                unitOfWork.Commit();
            }
            return View(new GridModel(GetFloor()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult DeleteFloor(int Id)
        {
            var model = floorRepository.Get(x => x.ID == Id);
            if(model!=null)
            {
                 floorRepository.Delete(model);
                 unitOfWork.Commit();
            }
           
            return View(new GridModel(GetFloor()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult UpdateFloor(int Id)
        {
            var model = floorRepository.Get(x => x.ID == Id);
            if (TryUpdateModel(model))
            {
                floorRepository.Update(model);
                unitOfWork.Commit();
            }

            return View(new GridModel(GetFloor()));
        }
    }
}
